package com.jus.player.fragment;

import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowInsets;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.core.view.ViewCompat;
import androidx.fragment.app.Fragment;

import xyz.doikki.videoplayer.player.VideoViewManager;
import xyz.doikki.videoplayer.util.L;

public abstract class BaseFragment extends Fragment {

    public View mRootView;

    public boolean mIsInitData;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        if (getActivity() == null) {
            throw new IllegalArgumentException("Activity no null");
        }
        if (mRootView == null) {
            mRootView = inflater.inflate(initLayout(), container, false);
            initView();
        }
        return mRootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!isLazyLoad()) {
            fetchData();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        fetchData();
        String simpleName = getClass().getSimpleName();
        L.d("BaseFragment " + simpleName + " onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        String simpleName = getClass().getSimpleName();
        L.d("BaseFragment " + simpleName + " onPause");
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        String simpleName = getClass().getSimpleName();
        L.d("BaseFragment " + simpleName + " onHiddenChanged " + hidden);
    }

    public <T extends View> T findViewById(@IdRes int id) {
        return mRootView.findViewById(id);
    }

    /**
     * 初始化布局
     *
     * @return
     */
    protected abstract int initLayout();

    /**
     * 初始化控件
     */
    protected abstract void initView();

    /**
     * 封装数据，接口方法，监听事件
     */
    protected abstract void initData();

    /**
     * 是否懒加载
     */
    protected boolean isLazyLoad() {
        return false;
    }

    private void fetchData() {
        if (mIsInitData) {
            return;
        }
        initData();
        mIsInitData = true;
    }

    /**
     * 子类可通过此方法直接拿到VideoViewManager
     */
    public VideoViewManager getVideoViewManager() {
        return VideoViewManager.instance();
    }

    /**
     * 把状态栏设成透明
     */
    protected void setStatusBarTransparent() {
        View decorView = getActivity().getWindow().getDecorView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            decorView.setOnApplyWindowInsetsListener((v, insets) -> {
                WindowInsets defaultInsets = v.onApplyWindowInsets(insets);
                return defaultInsets.replaceSystemWindowInsets(
                        defaultInsets.getSystemWindowInsetLeft(),
                        0,
                        defaultInsets.getSystemWindowInsetRight(),
                        defaultInsets.getSystemWindowInsetBottom());
            });
        }
        ViewCompat.requestApplyInsets(decorView);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getActivity().getWindow().setStatusBarColor(ContextCompat.getColor(getActivity(), android.R.color.transparent));
        }
    }

}
